package exception;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class TryFinallyThrow {
    public static void main(String[] args) throws IOException {
        /*
        FileOutputStream fos =new FileOutputStream("net.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        Scanner scan = new Scanner(System.in);
        System.out.println("请开始输入内容，单独输入exit时退出");
        while (true){
            String line = scan.nextLine();
            if ("exit".equals(line)){
                break;
            }
            pw.println(line);
        }

         */
        /*
        FileInputStream fis = new FileInputStream("pw.txt");
        InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();

         */
/*
        System.out.println("程序开始了");
        try {
            String line = "abc";
            System.out.println(line.charAt(2));
        }catch(NullPointerException|StringIndexOutOfBoundsException s){
            System.out.println("出现了字符串下标越界异常并解决了");
        }
        System.out.println("程序结束了");

 */
/*
        System.out.println("程序开始了");
        try{
            String line = "";
            System.out.println(line.length());
        }catch{
            System.out.println("出错并解决了");
        }finally{
            System.out.println("finally中的代码执行了");
        }
        System.out.println("程序结束了");
        */
        /*

        System.out.println("程序开始了");
        try(
            FileOutputStream fos = new FileOutputStream("fos.dat");
        ){
            fos.write(1);
        }catch (IOException e ){
            e.printStackTrace();
        }
        System.out.println("程序结束了");

        Student zs = new Student();
        try {
            zs.setAge(98);
            System.out.println("此人的年龄为："+zs.getAge()+"岁");
        }catch (Exception e){
            System.out.println("年龄不合法");
        }

         */
        /*

        System.out.println("程序开始了");
        FileOutputStream fos = null;
        try {
            fos=new FileOutputStream("./a/b/fos.dat");
            fos.write(1);
        }catch (IOException e){
            System.out.println("出现IO异常并解决了");
        }finally{
            try{
                fos.close();
            }catch (Exception e){
                System.out.println("关闭流时出现异常并解决了");
            }
        }
        System.out.println("程序结束了");

         */
         /*
        FileOutputStream fos =new FileOutputStream("net.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        BufferedWriter bw = new BufferedWriter(osw);
        PrintWriter pw = new PrintWriter(bw,true);
        Scanner scan = new Scanner(System.in);
        System.out.println("请开始输入内容，单独输入exit时退出");
        while (true){
            String line = scan.nextLine();
            if ("exit".equals(line)){
                break;
            }
            pw.println(line);
        }

         */
        /*
        FileInputStream fis = new FileInputStream("pw.txt");
        InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line=br.readLine())!=null){
            System.out.println(line);
        }
        br.close();

         */
/*
        System.out.println("程序开始了");
        try {
            String line = "abc";
            System.out.println(line.charAt(2));
        }catch(NullPointerException|StringIndexOutOfBoundsException s){
            System.out.println("出现了字符串下标越界异常并解决了");
        }
        System.out.println("程序结束了");

 */
/*
        System.out.println("程序开始了");
        try{
            String line = "";
            System.out.println(line.length());
        }catch{
            System.out.println("出错并解决了");
        }finally{
            System.out.println("finally中的代码执行了");
        }
        System.out.println("程序结束了");
        */
        /*

        System.out.println("程序开始了");
        try(
            FileOutputStream fos = new FileOutputStream("fos.dat");
        ){
            fos.write(1);
        }catch (IOException e ){
            e.printStackTrace();
        }
        System.out.println("程序结束了");

        Student zs = new Student();
        try {
            zs.setAge(98);
            System.out.println("此人的年龄为："+zs.getAge()+"岁");
        }catch (Exception e){
            System.out.println("年龄不合法");
        }

         */
        /*

        System.out.println("程序开始了");
        FileOutputStream fos = null;
        try {
            fos=new FileOutputStream("./a/b/fos.dat");
            fos.write(1);
        }catch (IOException e){
            System.out.println("出现IO异常并解决了");
        }finally{
            try{
                fos.close();
            }catch (Exception e){
                System.out.println("关闭流时出现异常并解决了");
            }
        }
        System.out.println("程序结束了");

         */
    }
}
